﻿#include "AccountBookMain.h"
#include "ui_AccountBookMain.h"
#include <QHBoxLayout>

AccountBookMain::AccountBookMain(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AccountBookMain)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);
    ui->stackedWidget_button->setCurrentIndex(0);

    // 底部功能切换按钮
    m_lstBottomBtn.append(ui->pushButton_item);
    m_lstBottomBtn.append(ui->pushButton_add);
    m_lstBottomBtn.append(ui->pushButton_chart);
    m_lstBottomBtn.append(ui->pushButton_save);
    for(int i = 0; i < m_lstBottomBtn.size(); ++i)
        connect(m_lstBottomBtn.at(i), SIGNAL(clicked()), this, SLOT(slotBottomBtnClicked()));

    // 添加账单-返回
    connect(ui->pushButton_return, SIGNAL(clicked()), this, SLOT(slotReturnMainPage()));

    // 账单添加页面
    QHBoxLayout *layout = new QHBoxLayout;
    layout->setMargin(0);
    m_accountBookAdd = new AccountBookAdd;
    layout->addWidget(m_accountBookAdd);
    ui->page_4->setLayout(layout);

    // 添加一条账单
    connect(this, SIGNAL(sigAddAccount()), m_accountBookAdd, SLOT(slotAccountAdd()));
    connect(m_accountBookAdd, SIGNAL(sigAddSuccessed()), this, SLOT(slotUpdateView()));


}

AccountBookMain::~AccountBookMain()
{
    delete ui;
}

void AccountBookMain::slotBottomBtnClicked()
{
    QPushButton *send = qobject_cast<QPushButton *>(sender());
    if(send == ui->pushButton_item)  // 明细
    {
        ui->stackedWidget->setCurrentIndex(0);
    }
    else if(send == ui->pushButton_add)  // 添加
    {
        ui->stackedWidget->setCurrentIndex(1);
        ui->stackedWidget_button->setCurrentIndex(1);
    }
    else if(send == ui->pushButton_chart)  // 统计
    {
        ui->stackedWidget->setCurrentIndex(2);
    }
    else if(ui->pushButton_save)  // 添加一条账单
    {
        emit sigAddAccount();
    }
}

void AccountBookMain::slotReturnMainPage()
{
    ui->stackedWidget_button->setCurrentIndex(0);
    ui->pushButton_item->click();
}

void AccountBookMain::slotUpdateView()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->stackedWidget_button->setCurrentIndex(0);

}

